<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作家登录</title>
    <script src="js/jquery.3.7.1min.js"></script>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: #f9f9f9;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }

        .login-container {
            width: 100%;
            max-width: 380px;
            padding: 40px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }

        .login-title {
            font-size: 24px;
            font-weight: 500;
            color: #333;
            margin-bottom: 30px;
            text-align: center;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-size: 14px;
            color: #666;
            margin-bottom: 8px;
        }

        /* 输入框和按钮统一盒模型和内边距 */
        .form-control,
        .btn-login {
            box-sizing: border-box;
            width: 100%;
            padding: 12px 15px;
            border-radius: 4px;
            font-size: 14px;
            outline: none;
            transition: all 0.2s;
        }

        .form-control {
            border: 1px solid #ddd;
            color: #333;
        }

        .form-control:focus {
            border-color: #0070f3;
        }

        /* 记住密码和忘记密码布局 */
        .auth-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: -10px;
            margin-bottom: 25px;
            font-size: 14px;
            color: #666;
        }

        .remember-me {
            display: flex;
            align-items: center;
        }

        .remember-me input {
            margin-right: 8px;
        }

        .forgot-password a {
            color: #0070f3;
            text-decoration: none;
        }

        .forgot-password a:hover {
            text-decoration: underline;
        }

        .btn-login {
            background-color: #0070f3;
            border: none;
            color: white;
            font-weight: 500;
            cursor: pointer;
        }

        .btn-login:hover {
            background-color: #005cc5;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <div class="login-title">作家登录</div>

        <form id="loginForm" action="/qimao/writer/loginWriter.do" method="POST">
            <div class="form-group">
                <label for="username">账号</label>
                <input type="text" id="username" class="form-control" name="username" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" class="form-control" name="password" placeholder="请输入密码" required>
            </div>

            <!-- 使用flex布局将记住密码和忘记密码放在同一行 -->
            <div class="auth-options">
                <div class="remember-me">
                    <input type="checkbox" id="rememberMe" name="rememberMe" value="true">
                    <label for="rememberMe">记住密码</label>
                </div>
                <div class="forgot-password">
                    <a href="#">忘记密码?</a>
                    <a href="regWriter.html">注册作家账号</a>
                </div>
            </div>
            <button type="submit" class="btn-login">登录</button>
        </form>
    </div>

    <script>
        // 设置Cookie
        function setCookie(name, value, days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            const expires = "expires=" + date.toUTCString();
            document.cookie = name + "=" + value + ";" + expires + ";path=/;SameSite=Lax";
        }

        // 获取Cookie
        function getCookie(name) {
            const cookieName = name + "=";
            const cookies = document.cookie.split(';');

            for (let i = 0; i < cookies.length; i++) {
                let cookie = cookies[i].trim();
                if (cookie.indexOf(cookieName) === 0) {
                    return cookie.substring(cookieName.length, cookie.length);
                }
            }
            return "";
        }

        // 页面加载时检查Cookie
        document.addEventListener('DOMContentLoaded', function () {
            const rememberMe = getCookie("rememberMe") === "true";

            if (rememberMe) {
                const username = getCookie("username");
                const password = getCookie("password");

                if (username && password) {
                    document.getElementById('username').value = username;
                    document.getElementById('password').value = password;
                    document.getElementById('rememberMe').checked = true;
                }
            }
        });

        // // 监听表单提交事件
        // $('#loginForm').on('submit', function (e) {
        //     // 阻止表单默认提交行为
        //     e.preventDefault();

        //     // 获取表单数据
        //     const formData = $(this).serialize();
        //     $.ajax({
        //         url: $(this).attr('action'), // 使用表单的 action 属性作为请求地址
        //         type: $(this).attr('method'), // 使用表单的 method 属性作为请求方法
        //         data: formData,
        //         dataType: 'json',
        //         success: function (response) {
        //         }

        //     });
        // });



        // 表单提交时处理Cookie
        document.getElementById('loginForm').addEventListener('submit', function (e) {
            const rememberMe = document.getElementById('rememberMe').checked;
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;


            if (rememberMe) {
                // 保存登录信息到Cookie（30天有效期）
                setCookie("rememberMe", "true", 30);
                setCookie("username", username, 30);
                setCookie("password", password, 30);
            } else {
                // 清除Cookie（设置为过去的时间）
                setCookie("rememberMe", "false", -1);
                setCookie("username", "", -1);
                setCookie("password", "", -1);
            }

            // 继续表单提交
            return true;
        });
    </script>
</body>

</html>